using Joy.Erp.BasicArchives.Localization;
using System.Linq;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Localization;

namespace Joy.Erp.Ufida.Permissions
{
    public class MaterialManagementPlatformPermissionDefinitionProvider : PermissionDefinitionProvider
    {
        public override void Define(IPermissionDefinitionContext context)
        {
            var menus = context.GetGroupOrNull(UfidaPermissions.Menus);
            if (menus == null)
            {
                menus = context.AddGroup(UfidaPermissions.Menus, L("Permission:Menus"));
            }

            var productionManagementMenu = menus.GetPermissionOrNull(UfidaPermissions.BasicArchivesMenu.ProductionManagement);
            if (productionManagementMenu == null)
            {
                productionManagementMenu = menus.AddPermission(UfidaPermissions.BasicArchivesMenu.ProductionManagement, L("Permission:Menus.BasicArchives.ProductionManagement"));
            }

            var parentMenu = productionManagementMenu.Children.FirstOrDefault(x => x.Name == UfidaPermissions.MaterialManagementPlatform.ParentMenu);
            if (parentMenu == null)
            {
                parentMenu = productionManagementMenu.AddChild(UfidaPermissions.MaterialManagementPlatform.ParentMenu, L("Permission:Menus.BasicArchives.ProductionManagement.Production"));
            }

            var menu = parentMenu.Children.FirstOrDefault(x => x.Name == UfidaPermissions.MaterialManagementPlatform.Menu);
            if (menu == null)
            {
                menu = parentMenu.AddChild(UfidaPermissions.MaterialManagementPlatform.Menu, L("Permission:Menus.BasicArchives.ProductionManagement.Production.MaterialManagementPlatform"));
            }

            var group = context.GetGroupOrNull(UfidaPermissions.GroupName);
            if (group == null)
            {
                group = context.AddGroup(UfidaPermissions.GroupName, L("Permission:BasicArchives"));
            }

            var pMaterialManagementPlatformDefault = group.AddPermission(UfidaPermissions.MaterialManagementPlatform.Default, L("Permission:BasicArchives.Production.MaterialManagementPlatform"));
            var pMaterialManagementPlatformQuery = pMaterialManagementPlatformDefault.AddChild(UfidaPermissions.MaterialManagementPlatform.Query, L("Permission:Query"));
            var pMaterialManagementPlatformExport = pMaterialManagementPlatformDefault.AddChild(UfidaPermissions.MaterialManagementPlatform.Edit, L("Permission:Edit"));
        }

        private static LocalizableString L(string name)
        {
            return LocalizableString.Create<BasicArchivesResource>(name);
        }
    }
}

